package aidansmeaton.airport;

import aidansmeaton.airport.structures.Airport;
import android.content.Context;
import android.database.Cursor;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class OpenRouteActivity extends CursorListActivity {
	
	private Airport fromAirport;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.open_route);
		
		cursor = db.getAirports();
		String iata = (String) getIntent().getExtras().get("iata");
		fromAirport = db.getAirport(iata);

		setListAdapter(new OpenRouteAdapter(this, cursor));
		
		ListView list = this.getListView();
		
		list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            	/*
            	TextView stopnameTextView = (TextView) view.findViewById(R.id.favourites_stopname);
            	CharSequence stopName = stopnameTextView.getText();
            	
            	BusStop stop = new BusStop((String) view.getTag(R.id.stopcode), stopName.toString(), true);
            	
            	qa = new BusStopListQuickAction(view, stop, FavouritesActivity.this);
            	qa.show();
            	*/
            }
		});
	}
	
	/**
	 * Adapter class for a database cursor to the
	 * users favourite bus stops.
	 * @author Euan Freeman
	 */
	private class OpenRouteAdapter extends CursorAdapter {
		
		private LayoutInflater inflater;
		private Location from;
		
		public OpenRouteAdapter(Context context, Cursor cursor) {
			super(context, cursor);
			
			inflater = LayoutInflater.from(context);
			from = new Location("EXAMPLE");
			from.setLatitude(fromAirport.getExactLocation().getLatitudeE6() / 1e6);
			from.setLongitude(fromAirport.getExactLocation().getLongitudeE6() / 1e6);
			
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			
			//view.setTag(R.id.stopcode, cursor.getString(DatabaseHelper.BUS_STOP_CODE));
			
			TextView airportName = (TextView) view.findViewById(R.id.openroute_airportname);
			TextView airportDistance = (TextView) view.findViewById(R.id.openroute_distance);		
			TextView airportDemand = (TextView) view.findViewById(R.id.openroute_demand);		
						
			airportName.setText(cursor.getString(2));
			
			Airport a = new Airport(cursor);
			float[] results = null;
			Location to = new Location("EXAMPLE");
			to.setLatitude(a.getExactLocation().getLatitudeE6() / 1e6);
			to.setLongitude(a.getExactLocation().getLongitudeE6() / 1e6);
			float distance = from.distanceTo(to);
			airportDistance.setText("" + (int) (distance / 1000) + "km");
			
			int demand = (int) ((distance/1000) * (a.getPassengers())/10000000);
			airportDemand.setText("" + demand);
			
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			return inflater.inflate(R.layout.open_route_row, null);
		}
	}

}
